package com.zhaotianhao.springboot_demo_fire.config;

import com.zhaotianhao.springboot_demo_fire.common.JsonResult;
import com.zhaotianhao.springboot_demo_fire.exception.FirearmException;
import com.zhaotianhao.springboot_demo_fire.exception.UserException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author Z-7
 * @Date 2022/3/24
 */
@Slf4j
@ControllerAdvice
public class GlobaExceptionlHandler{
    @ExceptionHandler(Exception.class)
    public @ResponseBody JsonResult exceptionHandler(Exception e){
        log.info("进入全局异常");
        return JsonResult.faild(500,"遇到异常，请联系后台开发",e.getMessage());
    }
    @ExceptionHandler(UserException.class)
    public @ResponseBody JsonResult userExceptionHandler(UserException e){
        log.info("登录时异常");
        return JsonResult.faild(e.getCode(),"登录模块异常，请联系后台开发",e.getMessage());
    }
    @ExceptionHandler(FirearmException.class)
    public @ResponseBody JsonResult firearmExceptionHandler(HttpServletRequest request,FirearmException e){
        log.info("枪械编辑时异常");
        return JsonResult.faild(e.getCode(),"枪械编辑异常，请联系后台开发",e.getMessage());
    }
}
